
package com.fit.hibernate;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;

import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;

import org.hibernate.criterion.Restrictions;

import com.fit.beans.BorrowBean;
import com.fit.beans.UserBean;
import com.fit.interfaces.BorrowI;
import com.fit.interfaces.User;
import com.fit.service.BorrowsI;

public class BorrowsServiceHibernate implements BorrowsI{

	private Session session;
	

	public BorrowsServiceHibernate(Session session) {
		this.session = session;
		
	}

	@Override
	public BorrowI save(BorrowI borrow) {
		// TODO Auto-generated method stub
		return (BorrowI) session.get(BorrowBean.class, session.save(borrow));
	}

	@Override
	public Collection<BorrowI> retrive() {
		Criteria criteria = session.createCriteria(BorrowBean.class);
		
		return criteria.list();
	}
	
	public Collection<BorrowI> retrive(Serializable id){
		
		
		Criteria criteria = session.createCriteria(BorrowBean.class).add(Restrictions.eq("user.id", id));
		return criteria.list();
	}

	@Override
	public BorrowI retriveSingleBorrow(Serializable id) {
		// TODO Auto-generated method stub
		return (BorrowI) session.get(BorrowBean.class, id);
	}

	@CommitAfter
	public BorrowI editSingleBorrow(BorrowI borrow) {

		return (BorrowI) session.merge(borrow);
	}

	
//	public Collection<BorrowI> retrivesingleBorrow(Date dateOfBorrow) {
//		Criteria criteria = (Criteria) session.createCriteria(BorrowBean.class).add(Restrictions.eq("dateOfBorrow", dateOfBorrow));
//		return criteria.list();
//	}

}
